﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Schwazzle.Core.Model;
using Schwazzle.Core.Services;

namespace Schwazzle.SchwazzleActivityFeed
{
    [ToolboxItemAttribute(false)]
    public class SchwazzleActivityFeed : WebPart
    {
        protected override void CreateChildControls()
        {
        }

        protected  void Render2(HtmlTextWriter writer)
        {
            writer.Write("<script src=\"http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js\" type=\"text/javascript\"></script>");
            writer.Write("<script src=\"http://vaiopony/Shared%20Documents/jsRender.js\" type=\"text/javascript\"></script>");

            writer.Write("<button id=\"switchBtn\">Show full details</button><br/><br />");

            writer.Write("<table>");
	        writer.Write("<tbody id=\"feedList\"></tbody>");
            writer.Write("</table>");
            writer.Write("<script type=\"text/javascript\">");
	        writer.Write("var feedEntries = [");
            IFeedService feedService = new RavenFeedService();
            IQueryable<FeedEntry> entries = feedService.GetUsersFeedEntry(this.Context.User.Identity.Name);
            string entriesText = string.Empty;
            foreach (FeedEntry entry in entries)
            {
                entriesText += "{ username: \"" + entry.Username + "\", entrydate: \"" + entry.EntryDate + "\", entryText: \"" + entry.FeedEntryText + "\" },";
            }
            if (entriesText.Length > 0)
            { entriesText = entriesText.Substring(0, entriesText.Length - 1); }
            writer.Write(entriesText);
		    writer.Write("];");
            writer.Write("$.templates({");
		    writer.Write("titleTemplate: \"<tr><td colspan=3>{{>username}}</td></tr>\"");
	        writer.Write("});");
            writer.Write(" $(document).ready(function() {");
            writer.Write("var html = $.render.titleTemplate( feedEntries );");
            writer.Write("$( \"#feedList\" ).html( html );");
            writer.Write("});");
            /*
             * 
            writer.Write("var details = false;");
            writer.Write("function switchTemplates() {");
            writer.Write("var html;");

            writer.Write("details = !details;");

            writer.Write("if ( details ) {");
            writer.Write("$( this ).text( \"Show titles only\" );");
            writer.Write("html = $.render.detailTemplate( feedEntries );");
            writer.Write("} else {");
            writer.Write("$( this ).text( \"Show full details\" );");
            writer.Write("html = $.render.titleTemplate( feedEntries );");
            writer.Write("}");
            writer.Write("$( \"#feedList\" ).html( html );");
            writer.Write("}");
            */
	        

	        //writer.Write("switchTemplates();");
            writer.Write("</script>");
            
            base.Render(writer);
        }
    }
}
